import { exportFileType } from "@/utils/status.js";
// 千分位
export const thousands = (num, len = 2) => {
	try {
		if (!num) return "0.00";
		return `${(num * 1).toFixed(len)}`.replace(
			/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,
			"$&,"
		);
	} catch (error) {
		return "0.00";
	}
};
/**
 * 千分位转数字
 * @param {*} num
 * @returns
 */
export const translateNumber = (num) => {
	try {
		if (num) {
			const val = num.replace(/,/g, "");
			return val * 1;
		}
		return "";
	} catch (error) {
		return num;
	}
};


/*  打开文件  */
export function openFile(url) {
	uni.showLoading()
	const file = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf']
	const picture = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp']
	const suffix = url.split('.').pop().toLocaleLowerCase()
	if (file.includes(suffix)) {
		// 打开文件
		uni.downloadFile({
			url,
			success: res => {
				if (res.statusCode == 200) {
					const filePath = res.tempFilePath
					uni.openDocument({
						filePath: filePath,
						success: function(res) {
							/* TODO 测试观测打印 */
							console.log("打开成功");
						}
					})
				}
				uni.hideLoading()
			},
			fail: () => {
				uni.hideLoading()
			}
		})
		return
	} else if (picture.includes(suffix)) {
		uni.hideLoading()
		uni.previewImage({
			urls: [url]
		})
	} else {
		uni.hideLoading()
		uni.navigateTo({
			url: `/pages/pdfView/index?urls=${url}`
		})
	}
}

/**
 * 业务中处理出口信息中的文件
 * @param {*} id //出口ID
 * @param {*} filterIds  // 过滤掉的的type 0-船运单,1-装箱照片,2-报关单,3-海运提单,4-箱单
 * @param {*} value // 文件数据
 */
export const getExportFiles = (value, id, filterIds = []) => {
	if (value.length <= 0) return [];
	const data = value.filter((x) => {
		return x.innerNo === id;
	});
	const list = JSON.parse(JSON.stringify(exportFileType));
	for (let i = 0; i < data.length; i++) {
		for (let j = 0; j < list.length; j++) {
			if (data[i].type === list[j].value && !filterIds.includes(data[i].type)) {
				list[j].list.push(data[i].fileUrl);
			}
		}
	}
	return list.filter((x) => {
		return x.list.length > 0;
	});
};